package com.sl.happying.reading.service;

import com.sl.happying.reading.bean.Chapter;
import com.sl.happying.reading.bean.Novel;
import com.sl.happying.reading.dao.ChapterDao;
import com.sl.happying.reading.dao.NovelDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.sql.rowset.serial.SerialClob;
import javax.transaction.Transactional;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class NovelServiceImpl implements NovelService {

    @Autowired
    private NovelDao novelDao;

    @Autowired
    private ChapterDao chapterDao;

    private final String REGEX = "第[1-9]\\d*章";

    @Override
    @Transactional
    public void resolveNovel(String context, String fileName) throws SQLException {
        //0 ： 如果数据库已经存在直接返回
        List<Novel> novelList = novelDao.findByNovelName(fileName);
        if (!CollectionUtils.isEmpty(novelList)) {
            return;
        }
        //1. save novel
        String novelName = fileName.replace(".txt", "");
        Novel currentNovel = new Novel(novelName, new Date());
        List<Chapter> chapters = new ArrayList<Chapter>();
        //2. save Chapter
        context = context.replaceAll("\\s*", "");
        Pattern pattern = Pattern.compile(REGEX);
        List<String> chapterContextList = new ArrayList<String>();
        Matcher matcher = pattern.matcher(context);
        while (matcher.find()) {
            chapterContextList.add(matcher.group());
        }
        int chapterSize = chapterContextList.size();
        for (int i = 0; i < chapterSize; i++) {
            String preChapterTitle = chapterContextList.get(i);
            int preIndex = context.indexOf(preChapterTitle);
            int j = i + 1;
            if (j < chapterSize) {
                String nextChapterTitle = chapterContextList.get(j);
                int nextIndex = j == (chapterSize - 1) ? context.length() : context.indexOf(nextChapterTitle);
                String chapterContext = context.substring(preIndex, nextIndex);
                //SerialClob chapterClob = new SerialClob(chapterContext.toCharArray());
                Chapter chapter = new Chapter(preChapterTitle, chapterContext, new Date());
                chapter.setNovel(currentNovel);
                chapters.add(chapter);
            }
        }
        currentNovel.setChapters(chapters);
        novelDao.save(currentNovel);
    }

    @Override
    public List<Novel> findAllNovel() {
        return novelDao.findAll();
    }

    @Override
    public void deleteNovel(String novelName) {
        List<Novel> novelList = novelDao.findByNovelName(novelName);
        if (CollectionUtils.isEmpty(novelList)) {
            return;
        }
        novelDao.delete(novelList.get(0));
    }

    @Override
    public List<Novel> findByNovelName(String novelName) {
        return novelDao.findByNovelName(novelName);
    }
}
